import Openid from '../22009-plugin/model/openid.js'
const prefix = '' // 野生机器人前缀
const Delay_ms = 100 // 指令延时
const group_qq = 531683972 // 触发的群聊
const QQBot_qq = { 102073196: 2854216359 } // 官方机器人qq号
const icqq_qq = 3158794729
const master_qq = 1902688707

/** 另外起一个监听器，直接源头监听QQBot */
Bot.on('message', async data => {
  if (data.adapter == 'QQBot') {
    const group_id = data.group_id
    const self_id = data.self_id
    const user_id = data.user_id
    const group = await Openid.Group.findOne({ where: { group_id, self_id } })
    if (!group || !group.qq) {
      updateGroupId(self_id, group_id, user_id)
    }
    if (data.self_id in QQBot_qq) {
      const user = await Openid.User.findOne({ where: { user_id, self_id: data.self_id } })
      if (!user || user.qq == 8888 || !user.qq) {
        try {
          Bot[icqq_qq].pickGroup(group_qq).sendMsg([segment.at(QQBot_qq[data.self_id]), `自动转换qq号${user_id}`])
        } catch (error) {}
      }
    }
  }
})

export class autoOpenIdtoId extends plugin {
  constructor () {
    super({
      name: '取qq号',
      dsc: '复读用户发送的内容，然后撤回',
      event: 'message',
      priority: -1000011,
      rule: [
        {
          reg: /^#?开始更新qq号$/i,
          fnc: 'sendOpenid'
        },
        {
          reg: '对应关系',
          fnc: 'writeOpenid'
        },
        {
          reg: '^#?启动对应转换',
          fnc: 'startOpenid'
        },
        {
          reg: '^#?自动转换(qq|QQ)号',
          fnc: 'sendOpenid_auto'
        }
      ]
    })
    this.task = {
      name: '控制icqq发起转换',
      fnc: () => this.startOpenid(),
      cron: '0 2 * * *'
    }
  }

  async startOpenid () {
    for (let self_id in QQBot_qq) {
      Bot[icqq_qq].pickGroup(group_qq).sendMsg([segment.at(QQBot_qq[self_id]), '开始更新qq号'])
      await sleep(5 * 60 * 1000)
    }
  }

  async sendOpenid_auto (e) {
    const openid = e.msg.replace(/^#?自动转换(qq|QQ)号/, '')
    await this.reply([`${prefix}对应关系\r${openid}`, segment.at(openid), segment.image(`https://q.qlogo.cn/qqapp/${openid.replace('-', '/')}/100`)])
  }

  async sendOpenid (e) {
    const limit = 50 // 一次更新50个用户
    let today = new Date()
    const DATE = today.setDate(today.getDate() - 1) // 固定更新前一天活跃的用户信息
    /** 固定根据日期和self_id筛选 */
    const where = {
      DATE, // 如果希望每次都更新全部用户，注释本行
      self_id: e.self_id
    }
    const cnt = await Openid.UserDAU.count({ where }) // 获取需要更新的数目
    for (let offset = 0; offset < cnt; offset += limit) { // offset为偏移量
      const users = await Openid.UserDAU.findAll({
        limit,
        offset,
        order: [['createdAt', 'DESC']], // 按照createdAt字段降序排列
        where
      })
      let msg = `${prefix}对应关系`
      users.forEach(user => {
        msg += `\n${user.user_id}\n<@${user.user_id.split('-')[1]}>`
      })
      await this.reply(msg)
      await sleep(Delay_ms)
    }
  }

  async writeOpenid (e) {
    const message = e.message.filter(item => item.type === 'text' || item.type === 'at')
    // console.log(message)
    for (let openid = 3; openid < message.length - 1; openid += 2) {
      const possition = message[openid].text.indexOf('对应关系')
      const user_id = message[openid].text.substring((possition == -1) ? 0 : (possition + 4)).trim()
      const self_id = user_id.split('-')[0]
      const updatedData = {
        user_id,
        qq: Number(message[openid + 1].qq),
        nickname: message[openid + 1].text.replace(/^@/, '').replace(/\\/g, ''),
        self_id
      }
      logger.debug('[22009]取qq', JSON.stringify(updatedData))
      if (!updatedData.qq) return
      Openid.UpdateUser(updatedData)
    }
  }
}

function sleep (ms) {
  return new Promise((resolve) => setTimeout(resolve, ms))
}

function image1x1 () {
  return 'base64:///9j/4QNRRXhpZgAATU0AKgAAAAgABwESAAMAAAABAAEAAAEaAAUAAAABAAAAYgEbAAUAAAABAAAAagEoAAMAAAABAAIAAAExAAIAAAAfAAAAcgEyAAIAAAAUAAAAkYdpAAQAAAABAAAAqAAAANQAACcQAAAnEAAAJxAAACcQQWRvYmUgUGhvdG9zaG9wIDIxLjIgKFdpbmRvd3MpADIwMjQ6MDQ6MDYgMTY6MjM6NDMAAAAAAAOgAQADAAAAAf//AACgAgAEAAAAAQAAAAGgAwAEAAAAAQAAAAEAAAAAAAAABgEDAA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GxmZgAGAAAAAAABAC9mZgABAKGZmgAGAAAAAAABADIAAAABAFoAAAAGAAAAAAABADUAAAABAC0AAAAGAAAAAAABOEJJTQP4AAAAAABwAAD/////////////////////////////A+gAAAAA/////////////////////////////wPoAAAAAP////////////////////////////8D6AAAAAD/////////////////////////////A+gAADhCSU0ECAAAAAAAEAAAAAEAAAJAAAACQAAAAAA4QklNBB4AAAAAAAQAAAAAOEJJTQQaAAAAAAM/AAAABgAAAAAAAAAAAAAAAQAAAAEAAAAFZypoB5iYAC0AMQAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAQAAAAEAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAQAAAAAAAG51bGwAAAACAAAABmJvdW5kc09iamMAAAABAAAAAAAAUmN0MQAAAAQAAAAAVG9wIGxvbmcAAAAAAAAAAExlZnRsb25nAAAAAAAAAABCdG9tbG9uZwAAAAEAAAAAUmdodGxvbmcAAAABAAAABnNsaWNlc1ZsTHMAAAABT2JqYwAAAAEAAAAAAAVzbGljZQAAABIAAAAHc2xpY2VJRGxvbmcAAAAAAAAAB2dyb3VwSURsb25nAAAAAAAAAAZvcmlnaW5lbnVtAAAADEVTbGljZU9yaWdpbgAAAA1hdXRvR2VuZXJhdGVkAAAAAFR5cGVlbnVtAAAACkVTbGljZVR5cGUAAAAASW1nIAAAAAZib3VuZHNPYmpjAAAAAQAAAAAAAFJjdDEAAAAEAAAAAFRvcCBsb25nAAAAAAAAAABMZWZ0bG9uZwAAAAAAAAAAQnRvbWxvbmcAAAABAAAAAFJnaHRsb25nAAAAAQAAAAN1cmxURVhUAAAAAQAAAAAAAG51bGxURVhUAAAAAQAAAAAAAE1zZ2VURVhUAAAAAQAAAAAABmFsdFRhZ1RFWFQAAAABAAAAAAAOY2VsbFRleHRJc0hUTUxib29sAQAAAAhjZWxsVGV4dFRFWFQAAAABAAAAAAAJaG9yekFsaWduZW51bQAAAA9FU2xp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AgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgPD94cGFja2V0IGVuZD0idyI/Pv/uACZBZG9iZQBkQAAAAAEDABUEAwYKDQAAAAAAAAAAAAAAAAAAAAD/2wCEAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQECAgICAgICAgICAgMDAwMDAwMDAwMBAQEBAQEBAQEBAQICAQICAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDA//CABEIAAEAAQMBEQACEQEDEQH/xABuAAEAAAAAAAAAAAAAAAAAAAAJAQEAAAAAAAAAAAAAAAAAAAAAEAEAAAAAAAAAAAAAAAAAAAAAEQEAAAAAAAAAAAAAAAAAAAAAEgEAAAAAAAAAAAAAAAAAAAAAEwEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAwIRAxEAAAF/D//aAAgBAQABBQJ//9oACAECAAEFAn//2gAIAQMAAQUCf//aAAgBAgIGPwJ//9oACAEDAgY/An//2gAIAQEBBj8Cf//aAAgBAQMBPyF//9oACAECAwE/IX//2gAIAQMDAT8hf//aAAwDAQMCEQMRAAAQH//aAAgBAQMBPxB//9oACAECAwE/EH//2gAIAQMDAT8Qf//Z'
}

export async function updateGroupId (self_id, group_id, operator_id) {
  const { target_id } = await Bot.uploadMedia(self_id, group_id, 'group', image1x1(), 'image')
  const updatedData = {
    group_id,
    qq: Number(target_id),
    self_id
  }
  if (!updatedData.qq) return
  Openid.UpdateGroup(updatedData)
  GroupChangeNotice('increase', Number(target_id), operator_id)
}

export async function GroupChangeNotice (sub_type, group_id, user_id) {
  const self_id = user_id.split('-')[0]
  sub_type = (sub_type === 'increase' ? '新增' : '减少')
  if (group_id && typeof group_id === 'string') {
    group_id = (await Openid.Group.findOne({ where: { group_id } }))?.qq
  }
  let operator_id = (await Openid.User.findOne({ where: { user_id } }))?.qq
  if (user_id && (!operator_id || operator_id == 8888)) {
    try {
      if (QQBot_qq[self_id]) {
        Bot[icqq_qq].pickGroup(group_qq).sendMsg([segment.at(QQBot_qq[self_id]), `自动转换qq号${user_id}`])
      }
    } catch (error) {}
    operator_id = user_id
  }
  const msg = [
    `[通知(${self_id}) - 群聊${sub_type}]\n群号：${group_id}\n操作人：${operator_id}`,
    segment.image(`https://p.qlogo.cn/gh/${group_id}/${group_id}/0`)
  ]
  try {
    Bot[icqq_qq].pickFriend(master_qq).sendMsg(msg)
  } catch (error) {}
}
